package com.oa.admin.controller;

import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.KeyAddVO;
import com.oa.admin.dto.KeyUpdateVO;
import com.oa.admin.entity.Key;
import com.oa.admin.service.IKeyService;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/key")
@Api(description = "钥匙表模块")
//@ApiIgnore
public class KeyController extends BaseController {

    private final IKeyService keyService;

    @Autowired
    public KeyController(IKeyService keyService) {
        this.keyService = keyService;
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/list")
    @CommonController
    @Description("列表")
    @ApiOperation(value = "列表")
    public Resp list(@RequestParam Integer pageNum, @RequestParam Integer pageSize) throws DataIsEmptyException {
        return new Resp().success(keyService.findKeysList(pageNum, pageSize));
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp findById(@ApiParam(value = "id", required = true) @PathVariable String id) throws DataIsEmptyException {
        return keyService.findKeysDetailsById(id);
    }


    /**
     * 新增
     *
     * @param key Key
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "钥匙表对象") @RequestBody(required = false) KeyAddVO keyAddVO) throws BusinessException {
        keyAddVO.setReserveMan(getLoginUserId());
        return keyService.addKeys(keyAddVO);
    }

    /**
     * 根据Id修改
     *
     * @param keyUpdateVO KeyUpdateVO
     * @return Resp
     */
    @PutMapping("")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "钥匙表对象") @RequestBody(required = false) KeyUpdateVO keyUpdateVO) throws BusinessException {
        return keyService.updateKeys(keyUpdateVO);
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        Key key = keyService.selectById(id);
        if (key != null && key.getIsDeleted() == 2) {
            key.setIsDeleted(1);
            if (keyService.deleteKey(id)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
